﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using ChessGameEngine;

namespace SilverlightChess
{
    public partial class ChessBoard : UserControl
    {
        private const string PathElementPattern =
            "<Path xmlns=\"http://schemas.microsoft.com/client/2007\" Data=\"{0}\" />";

        private readonly Color backgroundColor = Colors.Gray;
        private readonly Color highlightedColor = Colors.LightGray;
        private readonly Color noBackgroundColor = Colors.White;

        private readonly Dictionary<Type, string> pieces = new Dictionary<Type, string>
                                                                     {
                                                                         { typeof(Bishop), "M 172.39441 250.80202 C 152.86607 250.80202 143.10211 253.86519 143.10262 259.99158 C 143.10211 265.35211 151.71737 268.60681 168.94827 269.75546 L 200.53746 269.75546 C 238.82669 269.75546 257.97168 266.50075 257.9722 259.99158 C 257.97168 253.86519 248.20779 250.80202 228.68048 250.80202 L 172.39441 250.80202 M 172.39441 215.19237 C 155.54638 215.19237 146.16531 217.10693 144.25132 220.93585 C 143.48505 221.70173 143.10211 222.85036 143.10262 224.38199 C 143.10211 228.59385 147.12258 231.08268 155.16385 231.84852 C 165.11885 232.99715 180.24332 233.57156 200.53746 233.57156 C 220.83059 233.57156 236.14647 232.99715 246.48521 231.84852 C 254.14272 231.08268 257.97168 228.59385 257.9722 224.38199 C 257.97168 218.25559 248.20779 215.19237 228.68048 215.19237 L 172.39441 215.19237 M 279.22306 239.88929 L 290.70998 284.11412 C 290.70954 297.51561 277.1166 305.365 249.93128 307.66237 C 269.84155 315.32037 288.41217 319.1494 305.64302 319.1494 C 307.94001 319.1494 319.23547 318.57502 339.52953 317.42631 C 356.37667 317.42631 370.16098 326.23302 380.88263 343.84639 C 382.41374 346.90952 384.3283 350.35556 386.62611 354.18463 L 365.94961 385.77368 C 359.82271 393.43168 353.31345 398.4094 346.4218 400.70674 C 344.88966 392.66597 332.63695 387.68825 309.66355 385.77368 L 295.87915 384.62505 C 248.78215 380.79608 217.00151 370.64928 200.53746 354.18463 C 185.60393 369.8834 162.82142 378.88156 132.19002 381.17891 C 125.29783 381.94476 116.29975 383.09341 105.19565 384.62505 C 77.244019 386.92239 60.970827 390.94287 56.37612 396.68636 C 55.610285 397.83502 55.035896 399.17516 54.653026 400.70674 C 44.697775 396.49486 35.31668 386.92239 26.510019 371.98942 C 23.063877 365.86302 19.043488 359.92811 14.448687 354.18463 C 24.786971 335.03968 35.890974 323.55276 47.760812 319.72373 C 51.972812 318.19211 56.567521 317.42631 61.545214 317.42631 C 61.545214 317.42631 72.840712 318.00063 95.431776 319.1494 C 113.04505 319.1494 131.61562 315.32037 151.14346 307.66237 C 123.9577 305.365 110.36481 297.51561 110.36481 284.11412 C 110.36481 282.96545 113.42799 273.20159 119.55439 254.8224 C 121.46883 248.69601 122.42615 244.48411 122.42615 242.18673 C 99.069294 213.85229 87.390926 188.77239 87.390926 166.94719 C 87.390926 141.67588 99.835083 118.70202 124.72348 98.025527 C 146.93115 79.263379 163.01289 69.308069 172.9687 68.159417 C 164.54447 57.821146 160.33258 47.0999 160.33305 35.995893 C 160.33258 17.99966 169.33073 6.8956493 187.32743 2.6836882 C 191.53883 1.5354365 195.94219 0.96117695 200.53746 0.96068767 C 219.68181 0.96117695 232.12604 8.0443457 237.86997 22.211532 L 240.74172 34.272846 L 240.74172 35.995893 C 240.74126 46.717032 236.52936 57.438189 228.1061 68.159417 C 235.38069 70.073895 246.29327 76.008858 260.84396 85.964214 C 296.07015 110.46963 313.6835 137.46399 313.68394 166.94719 C 313.6835 189.1553 302.19646 213.46932 279.22306 239.88929 M 210.87569 186.47503 L 210.87569 160.05506 L 230.97782 160.05506 L 230.97782 138.22974 L 210.87569 138.22974 L 210.87569 113.53285 L 190.19919 113.53285 L 190.19919 138.22974 L 170.09697 138.22974 L 170.09697 160.05506 L 190.19919 160.05506 L 190.19919 186.47503 L 210.87569 186.47503 M 200.53746 24.508969 C 192.87896 24.508969 188.09269 27.954972 186.17866 34.84724 L 185.60442 38.293241 C 185.60393 45.568335 189.43287 50.163138 197.09134 52.077662 C 198.23953 52.460576 199.38824 52.651967 200.53746 52.651967 C 210.10938 52.651967 214.89564 47.865772 214.89613 38.293241 C 214.89564 29.103667 210.10938 24.508969 200.53746 24.508969 Z" },                                                                         
                                                                         { typeof(Rook), "M 41.70545 399.92283 L 28.724467 386.94184 L 28.724467 338.434 L 97.728545 270.11309 L 97.728545 134.15451 L 44.438342 81.547362 L 44.438342 20.741702 L 63.568161 1.61183 L 112.076 1.61183 L 124.37371 13.226502 L 124.37371 30.306689 L 150.33563 30.306689 L 150.33563 13.226502 L 161.95019 1.61183 L 237.78643 1.61183 L 249.40104 13.226502 L 249.40104 30.306689 L 275.36303 30.306689 L 275.36303 13.226502 L 286.97753 1.61183 L 336.16858 1.61183 L 355.29843 20.741702 L 355.29843 81.547362 L 302.00819 134.15451 L 302.00819 270.11309 L 371.01222 338.434 L 371.01222 386.94184 L 357.34807 399.92283 L 41.70545 399.92283 M 122.32408 254.39931 L 122.32408 277.62845 L 277.41265 277.62845 L 277.41265 254.39931 L 122.32408 254.39931 M 122.32408 131.42162 L 122.32408 154.65075 L 277.41265 154.65075 L 277.41265 131.42162 L 122.32408 131.42162 Z" },                                                                       
                                                                         { typeof(Knight), "M 92.802863 157.88685 C 103.88617 157.88685 115.63459 151.45841 128.04795 138.60174 C 134.69799 131.95176 138.02301 126.18832 138.02301 121.31165 C 138.02301 115.54821 132.70298 112.66655 122.06292 112.66655 C 109.6496 112.66655 99.231197 118.20828 90.807852 129.29164 C 86.374374 134.61172 84.157816 140.1534 84.157816 145.91683 C 84.157816 153.89683 87.039429 157.88685 92.802863 157.88685 M 81.49775 12.251171 C 82.384455 12.251171 84.601066 12.029469 88.147786 11.586116 C 108.09795 11.586116 129.37796 18.236255 151.98808 31.536277 C 154.64805 33.309637 155.97816 34.19624 155.97816 34.19624 C 168.83483 13.359476 178.58818 2.9411202 185.23821 2.9411202 C 199.42494 10.921164 208.7351 19.566211 213.16837 28.876211 C 215.38508 33.309637 218.7101 42.619585 223.14342 56.806362 C 226.69009 55.919605 230.23676 55.476407 233.78348 55.476407 C 271.02363 55.476407 306.49042 78.30813 340.18401 123.97171 C 380.52753 176.72865 400.69929 248.54884 400.69929 339.43264 C 400.69929 359.8261 399.81264 378.22445 398.03933 394.62794 L 117.40789 394.62794 C 118.29465 364.92442 129.15626 336.77267 149.99307 310.17248 C 152.20979 306.62581 155.3131 302.63584 159.30312 298.20246 C 175.70656 277.36575 185.68157 261.40566 189.22829 250.3222 C 191.0016 244.55892 192.3316 238.57384 193.21831 232.36715 C 163.95815 243.89387 142.01303 260.96231 127.38295 283.57243 C 120.73291 294.65574 113.41798 306.18256 105.43783 318.15257 C 103.22112 321.69929 99.896098 323.47265 95.462826 323.47265 C 84.379466 323.47265 78.837786 316.60096 78.837786 302.85754 C 74.404309 308.62087 69.305982 311.50259 63.542703 311.50259 C 63.542703 311.50259 59.109277 310.39418 50.24263 308.17757 C 31.179225 303.30079 17.657502 294.87744 9.677355 282.90738 C 7.017392 278.91741 4.1357267 273.59738 1.0323077 266.94739 C 4.1357267 247.88389 19.43081 209.97873 46.91761 153.23177 C 66.867721 112.4449 79.281139 83.184754 84.157816 65.45141 C 84.601066 61.461388 84.822768 55.698109 84.822768 48.161316 C 84.822768 41.954633 83.714411 29.984619 81.49775 12.251171 M 41.597581 245.66723 C 30.957523 245.66723 23.864134 250.3222 20.317465 259.6323 C 19.43081 261.84896 18.987457 264.06563 18.987457 266.28234 C 18.987457 268.05565 19.65241 270.27236 20.982468 272.93232 C 32.509181 272.93232 39.60257 268.05565 42.262533 258.30235 C 42.705938 256.97234 42.927588 255.42058 42.927588 253.64732 C 42.927588 252.76062 42.484236 250.10065 41.597581 245.66723 M 368.11406 348.07773 L 368.11406 323.47265 C 368.11406 251.20891 349.71571 192.02368 312.91891 145.91683 C 280.99864 106.01656 251.96018 86.066523 225.80349 86.066523 C 225.80349 86.066523 223.14342 85.62317 217.82339 84.736515 C 216.93674 85.179817 216.27168 85.62317 215.82833 86.066523 C 210.06505 86.066523 207.18339 88.504835 207.18339 93.38146 C 207.18339 95.154871 208.51339 97.371533 211.17336 100.0316 C 235.11344 102.24821 261.27028 119.31659 289.64373 151.23686 C 325.55388 192.91029 343.50903 253.20397 343.50903 332.11754 C 343.50903 332.11754 343.50903 333.44765 343.50903 336.10762 C 343.50903 355.61443 347.72065 365.36783 356.14399 365.36783 C 363.23744 365.36783 367.22751 359.6044 368.11406 348.07773 Z" },
                                                                         { typeof(King), "M 215.29717 248.28928 C 215.29717 255.57446 210.2381 259.21695 200.11997 259.21695 C 188.7876 259.21695 183.12145 255.57446 183.12145 248.28928 C 183.12145 214.69704 171.99142 187.37799 149.73156 166.33228 C 129.89997 148.52429 110.67545 139.62033 92.05806 139.62033 C 64.536662 139.62033 47.33579 150.54795 40.455439 172.40319 L 38.027081 188.79462 L 38.027081 190.0088 C 38.027081 206.19778 49.966503 228.66016 73.84536 257.39568 C 80.32099 264.68081 85.78479 270.34692 90.23678 274.39425 C 96.7124 279.65561 107.43766 282.28632 122.4125 282.28632 C 138.19686 282.28632 163.6946 279.25087 198.90572 273.18002 C 234.52169 279.25087 260.22182 282.28632 276.00611 282.28632 C 291.38572 282.28632 302.11092 279.65561 308.18191 274.39425 C 325.18039 259.41929 339.34581 241.20664 350.67807 219.75616 C 357.15376 207.61431 360.39155 197.69857 360.39155 190.0088 C 360.39155 164.51101 348.85679 148.52429 325.78744 142.04874 C 319.71653 140.42981 313.24094 139.62033 306.36061 139.62033 C 287.74327 139.62033 268.72108 148.72663 249.29414 166.93943 C 226.62952 187.17574 215.29717 214.29234 215.29717 248.28928 M 347.03557 190.0088 C 347.03557 206.19778 335.90565 225.21992 313.64564 247.07515 L 297.86133 260.43122 L 291.79044 264.07367 C 284.10063 265.28785 276.00611 265.89494 267.5069 265.89494 C 242.41387 265.89494 229.86732 260.83583 229.86732 250.7177 C 230.67674 219.5538 239.9855 195.27017 257.79344 177.86704 C 273.17301 162.48736 288.95739 154.79758 305.14636 154.79758 C 325.78744 154.79758 338.94111 162.28506 344.60717 177.25991 L 347.03557 190.0088 M 289.36213 344.20949 C 289.36213 333.68658 259.61472 328.42516 200.11997 328.42516 C 183.93091 328.42516 169.56311 329.03221 157.01661 330.24642 L 142.44647 331.4606 C 120.59125 334.2937 109.66361 338.3409 109.66361 343.60234 C 109.66361 349.67324 115.12741 352.70871 126.05506 352.70871 L 199.51288 346.6378 L 272.36367 352.70871 C 283.69586 352.70871 289.36213 349.87564 289.36213 344.20949 M 323.35912 358.7796 C 322.54963 386.30103 281.26758 400.06171 199.51288 400.06171 C 118.5676 400.06171 77.487885 386.30103 76.273729 358.7796 C 75.869007 332.87712 72.024114 309.80777 64.739018 289.57146 C 60.287036 278.64387 51.787783 264.68081 39.24126 247.68229 C 23.456941 226.23175 15.564772 207.20966 15.564772 190.61584 C 15.564772 164.71337 26.694752 144.67945 48.954691 130.514 C 61.501203 122.41949 75.464273 118.37225 90.84388 118.37225 C 111.88964 118.37225 131.92358 126.06207 150.94571 141.4416 C 150.94571 122.82422 152.76699 110.07534 156.40955 103.19505 C 159.24259 95.50518 168.55128 86.196435 184.33561 75.268866 L 184.33561 61.305798 L 159.44499 46.128549 L 159.44499 24.273313 L 181.90728 24.273313 L 189.19236 1.8110802 L 209.83335 1.8110802 L 217.72552 24.273313 L 239.58072 24.273313 L 239.58072 46.128549 L 215.29717 61.305798 L 215.29717 75.268866 C 230.67674 86.601178 240.18781 96.314617 243.83033 104.40918 L 248.07996 122.01469 C 248.48477 126.87141 248.88944 133.3471 249.29414 141.4416 C 268.3163 126.06207 287.94555 118.37225 308.18191 118.37225 C 335.29852 118.37225 356.34432 128.69274 371.31916 149.33378 C 379.81849 161.47557 384.06805 175.23625 384.06805 190.61584 C 384.06805 207.20966 376.17591 226.4341 360.39155 248.28928 L 346.4285 267.71626 C 331.45369 290.38088 323.7639 320.73538 323.35912 358.7796 M 51.990139 190.0088 C 51.990139 173.01032 60.489391 162.0827 77.487885 157.22599 C 82.3446 155.60702 87.40371 154.79758 92.66512 154.79758 C 109.66361 154.79758 125.85269 162.48736 141.23228 177.86704 C 158.6355 195.67496 167.74186 219.95855 168.55128 250.7177 C 168.95602 259.6217 159.64734 264.68081 140.62519 265.89494 C 137.79216 265.89494 134.75666 265.89494 131.51886 265.89494 C 123.42434 265.89494 115.12741 265.28785 106.62817 264.07367 C 93.27221 257.59803 79.916255 244.84924 66.560286 225.82705 C 56.846855 211.66164 51.990139 199.72224 51.990139 190.0088 Z" },
                                                                         { typeof(Queen), "M 105.02724 325.06213 C 105.02724 331.19853 109.43774 334.2667 118.2588 334.2667 L 157.95346 331.39028 C 157.95297 331.39028 169.45868 330.81494 192.47059 329.66442 L 201.09987 329.66442 L 268.40819 333.11618 L 282.21504 334.2667 C 291.0355 334.2667 295.44617 331.19853 295.44661 325.06213 C 295.44617 313.94001 269.75002 307.80362 218.35835 306.65304 L 200.52454 306.65304 C 157.18589 306.65304 128.23036 309.91306 113.65648 316.43285 C 107.90367 318.73404 105.02724 321.61046 105.02724 325.06213 M 33.116589 84.592977 C 25.829577 84.592977 21.227382 88.236526 19.309734 95.523447 C 18.926195 96.674017 18.734404 97.824544 18.734404 98.975204 C 18.734404 106.26212 22.377954 110.86441 29.66487 112.78197 C 30.815395 113.1655 31.965972 113.3573 33.116589 113.3573 C 42.321109 113.3573 46.923395 108.56322 46.923395 98.975204 C 46.923395 89.387052 42.321109 84.592977 33.116589 84.592977 M 108.47886 48.350032 C 98.507308 48.350032 93.521569 53.144108 93.521569 62.732171 C 93.521569 72.320323 98.507308 77.114309 108.47886 77.114309 C 117.68348 77.114309 122.28575 72.320323 122.28575 62.732171 C 122.28575 53.144108 117.68348 48.350032 108.47886 48.350032 M 291.99485 48.350032 C 285.47447 48.350032 281.06393 51.610043 278.76334 58.129886 L 278.188 62.732171 C 278.18755 72.320323 282.78984 77.114309 291.99485 77.114309 C 301.58242 77.114309 306.3765 72.320323 306.37695 62.732171 C 306.3765 53.144108 301.58242 48.350032 291.99485 48.350032 M 366.78193 84.592977 C 357.57683 84.592977 352.97463 89.387052 352.97512 98.975204 C 352.97463 108.56322 357.57683 113.3573 366.78193 113.3573 C 376.36955 113.3573 381.16354 108.56322 381.16411 98.975204 C 381.16354 91.688283 377.52012 87.085955 370.23365 85.168306 C 369.08263 84.784768 367.93201 84.592977 366.78193 84.592977 M 325.36137 359.57931 C 325.36088 370.31794 305.60947 377.79666 266.1071 382.01545 L 210.30435 385.46707 L 199.94921 385.46707 C 153.15893 385.46707 117.49169 381.44012 92.94624 373.38617 C 81.056993 369.93441 75.112386 365.33213 75.112386 359.57931 C 75.112386 348.45714 78.180562 335.80081 84.316957 321.61046 C 88.5357 313.17293 90.645138 307.22829 90.645138 303.77666 C 90.645138 296.48974 81.824068 282.29926 64.18196 261.20558 C 54.593898 250.08335 49.032833 242.41285 47.498724 238.19407 C 47.498724 235.89297 46.348158 230.7154 44.046964 222.66145 L 36.568257 193.89717 C 31.582522 175.10449 28.322558 155.16138 26.788446 134.06752 C 9.1463853 126.78061 0.3253567 115.08316 0.3253567 98.975204 C 0.3253567 85.168306 6.6534082 75.388497 19.309734 69.635686 C 23.52848 67.717993 28.130761 66.759169 33.116589 66.759169 C 45.772828 66.759169 55.169186 72.512069 61.305529 84.01778 C 63.990256 89.003514 65.332578 93.989381 65.332578 98.975204 L 62.456147 120.836 C 62.456147 138.09452 74.728802 159.18833 99.274378 184.11736 C 101.19194 168.3929 102.15082 151.90141 102.15082 134.64276 C 102.15082 108.94676 97.548525 92.647019 88.343954 85.743637 C 87.57692 84.976515 86.809804 84.401319 86.042852 84.01778 C 79.139383 79.79899 75.687715 72.320323 75.687715 61.581644 C 75.687715 47.391297 82.207518 37.611399 95.247341 32.242082 C 99.466168 30.324478 103.87667 29.365654 108.47886 29.365654 C 121.90222 29.365654 131.49032 35.502092 137.24317 47.774746 C 139.54428 51.993581 140.69436 56.595776 140.69489 61.581644 L 136.66784 86.318878 C 136.66784 107.79623 147.21429 140.58736 168.3086 184.69264 C 176.36206 148.6414 180.38915 121.0278 180.38959 101.85159 C 180.38915 85.743637 177.51263 70.977959 171.76036 57.55469 C 169.45868 52.568822 168.30815 48.733615 168.3086 46.048935 C 168.30815 31.858544 174.6362 22.078733 187.29298 16.709327 C 191.51123 14.791768 195.92176 13.8329 200.52454 13.8329 C 213.94742 13.8329 223.34364 20.161085 228.71349 32.817412 C 230.6306 37.036158 231.58947 41.44665 231.58991 46.048935 C 231.58947 46.815967 228.32951 56.979359 221.81011 76.539068 C 220.27546 81.524802 219.50847 89.962383 219.50892 101.85159 C 219.50847 121.0278 223.53543 148.6414 231.58991 184.69264 C 252.29971 140.9709 262.65489 108.17968 262.65534 86.318878 L 259.20363 62.156974 L 259.20363 61.581644 C 259.20309 47.391297 265.72306 37.611399 278.76334 32.242082 C 282.98158 30.324478 287.39212 29.365654 291.99485 29.365654 C 305.41772 29.365654 315.00579 35.502092 320.75913 47.774746 C 323.05978 51.993581 324.21036 56.595776 324.2108 61.581644 C 324.21036 72.320323 320.7586 79.990781 313.85566 84.592977 C 303.11654 91.496492 297.74726 108.37147 297.7477 135.21809 C 297.74726 152.09316 298.706 168.3929 300.62414 184.11736 C 325.16909 159.18833 337.44187 138.09452 337.44237 120.836 L 334.56594 98.975204 C 334.56549 85.551845 340.70189 75.963738 352.97512 70.210882 C 357.19338 67.909785 361.79567 66.759169 366.78193 66.759169 C 380.58834 66.759169 390.36815 72.895518 396.12144 85.168306 L 399.57316 96.098776 L 399.57316 98.975204 C 399.57272 114.31604 390.75164 126.01348 373.11007 134.06752 C 370.80844 160.53066 364.28855 192.93839 353.55036 231.29068 C 352.78288 234.3589 352.3993 236.66 352.39979 238.19407 C 352.3993 240.87874 349.52288 245.28928 343.77051 251.42568 L 336.86712 260.05492 C 318.4575 281.14874 309.25293 295.72262 309.25338 303.77666 C 309.25293 306.07776 313.85521 317.96701 323.06027 339.44432 C 324.59394 347.11474 325.36088 353.82646 325.36137 359.57931 M 200.52454 30.516226 C 193.23709 30.516226 188.6348 34.15973 186.71765 41.44665 C 186.33362 42.597222 186.14196 43.747748 186.14245 44.898319 C 186.14196 52.185284 189.78537 56.787568 197.07278 58.705216 C 198.22287 59.088754 199.37352 59.280546 200.52454 59.280546 C 209.72866 59.280546 214.33087 54.486471 214.33139 44.898319 C 214.33087 37.994937 211.07098 33.392608 204.5515 31.091511 C 203.40048 30.708017 202.05816 30.516226 200.52454 30.516226 M 304.07585 254.87744 C 304.0754 242.60465 269.55827 236.46821 200.52454 236.46821 C 131.10678 236.46821 96.397949 242.79639 96.397949 255.45272 C 96.397949 260.82199 101.38369 263.50667 111.3553 263.50667 C 122.86108 263.50667 137.24317 262.54789 154.50174 260.63025 L 191.89526 259.47972 L 201.09987 259.47972 C 211.83797 259.47972 232.16467 260.43845 262.08005 262.35614 L 290.269 263.50667 C 299.47312 263.50667 304.0754 260.63025 304.07585 254.87744 Z" },                                                                       
                                                                         { typeof(Pawn), "M 53.5592 399.86014 C 53.106143 394.42176 52.879509 383.7716 52.879509 367.90969 C 52.879509 310.8068 70.554279 268.43282 105.90356 240.78784 C 116.32715 232.17706 129.01613 224.24613 143.97223 216.99495 C 126.29752 211.55657 114.28781 198.18727 107.943 176.88699 C 106.13019 170.08902 105.22387 163.06447 105.22387 155.81328 C 105.22387 125.44901 120.85914 103.9222 152.12974 91.232584 C 143.9716 77.6367 139.89287 64.72056 139.89345 52.484217 C 139.89287 32.090364 149.86325 16.908257 169.80451 6.9378859 C 178.86782 2.4058895 188.61161 0.1398649 199.03573 0.1398649 C 221.69508 0.1398649 238.46331 8.9772765 249.34064 26.651989 C 254.32522 34.356336 256.81788 42.967113 256.8184 52.484217 C 256.81788 65.626991 252.96568 78.543078 245.26181 91.232584 C 262.02967 98.030552 274.03938 107.09455 281.29104 118.4244 C 288.54164 130.6608 292.16716 143.12367 292.16789 155.81328 C 292.16716 179.37949 284.00965 197.28084 267.69515 209.51713 C 263.61579 213.14275 259.0838 215.63535 254.09916 216.99495 C 295.79277 238.29523 322.75812 266.1669 334.99499 300.6099 C 341.33916 319.64416 344.51151 342.07741 344.51204 367.90969 C 344.51151 383.7716 344.28488 394.42176 343.83235 399.86014 L 53.5592 399.86014 Z" },                                                                         
                                                                     };

        private readonly Color selectedColor = Colors.Green;
        private ChessEngine chessEngine;

        public ChessBoard()
        {
            InitializeComponent();
            InitBoard();
            CompositionTarget.Rendering += MainLoop;
        }

        private void MainLoop(object sender, EventArgs e)
        {
            DrawBoard();
        }

        public void InitBoard()
        {
            chessEngine = new ChessEngine(Board.Width, Board.Height);

            CreatePiece<Rook>(PieceColor.White, new TilePosition { I = 0, J = 0 });
            CreatePiece<Rook>(PieceColor.White, new TilePosition { I = 7, J = 0 });
            CreatePiece<Rook>(PieceColor.Black, new TilePosition { I = 0, J = 7 });
            CreatePiece<Rook>(PieceColor.Black, new TilePosition { I = 7, J = 7 });

            //CreatePiece<Knight>(PieceColor.White, new TilePosition { I = 1, J = 0 });
            //CreatePiece<Knight>(PieceColor.White, new TilePosition { I = 6, J = 0 });
            //CreatePiece<Knight>(PieceColor.Black, new TilePosition { I = 1, J = 7 });
            //CreatePiece<Knight>(PieceColor.Black, new TilePosition { I = 6, J = 7 });

            //CreatePiece<Bishop>(PieceColor.White, new TilePosition { I = 2, J = 0 });
            //CreatePiece<Bishop>(PieceColor.White, new TilePosition { I = 5, J = 0 });
            //CreatePiece<Bishop>(PieceColor.Black, new TilePosition { I = 2, J = 7 });
            //CreatePiece<Bishop>(PieceColor.Black, new TilePosition { I = 5, J = 7 });

            CreatePiece<King>(PieceColor.White, new TilePosition { I = 4, J = 0 });
            //CreatePiece<Queen>(PieceColor.White, new TilePosition { I = 3, J = 0 });

            CreatePiece<King>(PieceColor.Black, new TilePosition { I = 4, J = 7 });
            //CreatePiece<Queen>(PieceColor.Black, new TilePosition { I = 3, J = 7 });

            //for (int i = 0; i < ChessEngine.MaxTiles; i++)
            //{
            //    CreatePiece<Pawn>(PieceColor.White, new TilePosition { I = i, J = 1 });
            //    CreatePiece<Pawn>(PieceColor.Black, new TilePosition { I = i, J = 6 });
            //}

            for (int i = 0; i < ChessEngine.MaxTiles; i++)
            {
                var x = i*chessEngine.TileWidth;
                for (int j = 0; j < ChessEngine.MaxTiles; j++)
                {
                    var y = j*chessEngine.TileHeight;
                    var color = GetTileBackgroundColor(i, j);
                    var tile = CreateTile(x, y, chessEngine.TileWidth, chessEngine.TileHeight, color);
                    chessEngine.BoardState[i, j].Geometry = tile;
                    chessEngine.BoardState[i, j].Background = color == backgroundColor;
                    Board.Children.Add(tile);
                }
            }
        }

        private void CreatePiece<T>(PieceColor color, TilePosition position)
        {
            Type type = typeof (T);
            var pathElementString = string.Format(PathElementPattern, pieces[typeof(T)]);
            var pathElement = (Path)XamlReader.Load(pathElementString);

            pathElement.RenderTransform = new ScaleTransform { ScaleX = 0.1, ScaleY = 0.1 };
            pathElement.Style =
                (Style)Application.Current.Resources[color == PieceColor.Black ? "BlackStyle" : "WhiteStyle"];
            Board.Children.Add(pathElement);
            
            Piece piece;
            switch (typeof(T).Name)
            {
                case "Rook":
                    piece = new Rook(chessEngine, pathElement, color, position);
                    chessEngine.PlacePieceOnBoard(piece, position);
                    break;
                case "Pawn":
                    piece = new Pawn(chessEngine, pathElement, color, position);
                    chessEngine.PlacePieceOnBoard(piece, position);
                    break;
                default:
                    piece = (Piece)Activator.CreateInstance(typeof(T), chessEngine, pathElement, color);
                    chessEngine.PlacePieceOnBoard(piece, position);
                    break;
            }                     
        }

        private Color GetTileBackgroundColor(int i, int j)
        {
            return !(j%2 == 0 ^ i%2 == 0) ? noBackgroundColor : backgroundColor;
        }

        public void DrawBoard()
        {
            for (int i = 0; i < ChessEngine.MaxTiles; i++)
            {
                var x = (double) i*chessEngine.TileWidth;
                for (int j = 0; j < ChessEngine.MaxTiles; j++)
                {
                    var y = (double) j*chessEngine.TileHeight;
                    var tile = chessEngine.BoardState[i, j].Geometry as Rectangle;
                    if (tile != null)
                    {
                        if (chessEngine.BoardState[i, j].Background)
                            tile.Fill = new SolidColorBrush { Color = backgroundColor };
                        else
                            tile.Fill = new SolidColorBrush { Color = noBackgroundColor };

                        if (chessEngine.BoardState[i, j].Highlighted)
                            tile.Fill = new SolidColorBrush { Color = highlightedColor };

                        if (chessEngine.BoardState[i, j].Selected)
                            tile.Fill = new SolidColorBrush { Color = selectedColor };
                    }
                    var piece = chessEngine.BoardState[i, j].Piece;
                    if (piece != null)
                    {
                        var shape = piece.UserInterfaceInstance as Path;
                        if (shape != null)
                        {
                            const int pieceOffset = 5;
                            if (piece.Position == TilePosition.Default)
                                shape.Visibility = Visibility.Collapsed;

                            shape.SetValue(Canvas.TopProperty, y + pieceOffset);
                            shape.SetValue(Canvas.LeftProperty, x + pieceOffset);
                        }
                    }
                }
            }
        }

        public Rectangle CreateTile(double x, double y, double width, double height, Color color)
        {
            var rectangle = new Rectangle { Height = height, Width = width };
            var fillColor = new SolidColorBrush { Color = color };
            var border = new SolidColorBrush { Color = Colors.Black };

            // Set border width and color
            rectangle.StrokeThickness = 1;
            rectangle.Stroke = border;

            // Fill rectangle with the given fill color
            rectangle.Fill = fillColor;
            //rectangle.Fill = new ImageBrush();

            // Set the x and y position (related to the the canvas left and top values)
            rectangle.SetValue(Canvas.LeftProperty, x);
            rectangle.SetValue(Canvas.TopProperty, y);

            return rectangle;
        }

        private void MouseMoved(object sender, MouseEventArgs e)
        {
            var point = e.GetPosition(Board);
            var pos = GetTilePositionFor(point);
            chessEngine.UpdatePieceMovementTracking(pos);
        }

        private TilePosition GetTilePositionFor(Point point)
        {
            return chessEngine.GetTilePositionFor(point.X, point.Y);
        }

        private void MouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            var point = e.GetPosition(Board);
            var pos = GetTilePositionFor(point);
            chessEngine.UpdatePieceMovement(pos);
        }
    }
}